package com.deliveryservicesystem.presentation.desktop;

import java.awt.print.PrinterException;
import java.io.FileReader;
import java.io.IOException;
import java.text.MessageFormat;
import javax.swing.*;

/**
 * File Name: RunClientApp.java
 * Version: 1.0
 * Description: This the clients print frame
 * Package: videoappclient
 * Author: Lance Reid (210 122 919)
 * Devon Richards (210 152 885)
 * Dwayne Peterson (210 019 727)
 * Date: Oct 25, 2011 1:31:55 PM
 */
public class PrintTransaction extends javax.swing.JFrame
{

    private static final long serialVersionUID = 1L;

    /**
     * Default constructor
     */
    public PrintTransaction()
    {
        initComponents();
        text.setTabSize(2);
        load(text, "Out.txt");
        //load(guideText, "Out.txt");
    }

    /**
     * This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents()
    {
        jScrollPane1 = new javax.swing.JScrollPane();
        text = new javax.swing.JTextArea();
        headerLabel = new javax.swing.JLabel();
        headerField = new javax.swing.JTextField();
        footerLabel = new javax.swing.JLabel();
        footerField = new javax.swing.JTextField();
        interactiveCheck = new javax.swing.JCheckBox();
        printButton = new javax.swing.JButton();
        guideText = new javax.swing.JTextArea();
        backgroundCheck = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Print");
        text.setColumns(20);
        text.setRows(5);
        text.setMargin(new java.awt.Insets(2, 2, 2, 2));
        jScrollPane1.setViewportView(text);

        headerLabel.setText("Header");

        headerField.setText("Report");

        footerLabel.setText("Footer");

        footerField.setText("Page {0}");

        interactiveCheck.setSelected(true);
        interactiveCheck.setText("Show Progress Dialog");
        interactiveCheck.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));

        printButton.setText("Print!");
        printButton.addActionListener(new java.awt.event.ActionListener()
        {

            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                print(evt);
            }
        });

        guideText.setColumns(20);
        guideText.setEditable(false);
        guideText.setLineWrap(true);
        guideText.setRows(5);
        guideText.setWrapStyleWord(true);
        guideText.setOpaque(false);

        backgroundCheck.setSelected(true);
        backgroundCheck.setText("Print in Background");
        backgroundCheck.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false).addComponent(guideText).addGroup(layout.createSequentialGroup().addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false).addComponent(backgroundCheck, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(interactiveCheck, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(printButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)).addGroup(layout.createSequentialGroup().addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false).addComponent(footerLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(headerLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false).addComponent(footerField).addComponent(headerField, javax.swing.GroupLayout.PREFERRED_SIZE, 209, javax.swing.GroupLayout.PREFERRED_SIZE)))).addGap(15, 15, 15).addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 343, Short.MAX_VALUE).addContainerGap()));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 403, Short.MAX_VALUE).addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addComponent(guideText, javax.swing.GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE).addGap(18, 18, 18).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(headerLabel).addComponent(headerField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(footerLabel).addComponent(footerField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)).addGap(27, 27, 27).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addComponent(interactiveCheck, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(backgroundCheck, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)).addComponent(printButton, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)))).addContainerGap()));
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void load(JTextArea comp, String fileName)
    {
        try
        {
            comp.read(new FileReader(fileName), null);
        }
        catch (IOException ex)
        {
            System.out.println(ex.toString());
        }
    }

    private void print(java.awt.event.ActionEvent evt)
    {//GEN-FIRST:event_print
        MessageFormat header = createFormat(headerField);
        MessageFormat footer = createFormat(footerField);
        boolean interactive = interactiveCheck.isSelected();
        boolean background = backgroundCheck.isSelected();

        PrintingTask task = new PrintingTask(header, footer, interactive);
        if (background)
        {
            task.execute();
        }
        else
        {
            task.run();
        }
    }//GEN-LAST:event_print

    /**
     * private inner class to manage the print job
     */
    private class PrintingTask extends SwingWorker<Object, Object>
    {

        private final MessageFormat headerFormat;
        private final MessageFormat footerFormat;
        private final boolean interactive;
        private volatile boolean complete = false;
        private volatile String message;

        /**
         * Constructor
         */
        public PrintingTask(MessageFormat header, MessageFormat footer, boolean interactive)
        {
            this.headerFormat = header;
            this.footerFormat = footer;
            this.interactive = interactive;
        }

        @Override
        protected Object doInBackground()
        {
            try
            {
                complete = text.print(headerFormat, footerFormat, true, null, null, interactive);
                message = "Printing " + (complete ? "complete" : "canceled");
            }
            catch (PrinterException ex)
            {
                message = "Sorry, a printer error occurred";
            }
            catch (SecurityException ex)
            {
                message = "Sorry, cannot access the printer due to security reasons";
            }
            return null;
        }

        @Override
        protected void done()
        {
            message(!complete, message);
        }
    }

    /**
     * Creates the formate of the message to print
     */
    private MessageFormat createFormat(JTextField source)
    {
        String txt = source.getText();
        if (txt != null && txt.length() > 0)
        {
            try
            {
                return new MessageFormat(txt);
            }
            catch (IllegalArgumentException e)
            {
                error("Sorry, this format is invalid.");
            }
        }
        return null;
    }

    /**
     * Print a JOptionPane message
     */
    private void message(boolean error, String msg)
    {
        int type = (error ? JOptionPane.ERROR_MESSAGE : JOptionPane.INFORMATION_MESSAGE);
        JOptionPane.showMessageDialog(this, msg, "Printing", type);
    }

    /**
     * Print error JOptionPane massage
     */
    private void error(String msg)
    {
        message(true, msg);
    }

    /**
     * Create the GUI
     */
    public static void createAndShowGUI()
    {
        JFrame f = new PrintTransaction();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    /**
     * Start the application
     */
    public void startPrintTransaction()
    {
        SwingUtilities.invokeLater(new Runnable()
        {

            @Override
            public void run()
            {
                //Turn off metal's use of bold fonts
                UIManager.put("swing.boldMetal", Boolean.FALSE);
                createAndShowGUI();
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox backgroundCheck;
    private javax.swing.JTextField footerField;
    private javax.swing.JLabel footerLabel;
    private javax.swing.JTextArea guideText;
    private javax.swing.JTextField headerField;
    private javax.swing.JLabel headerLabel;
    private javax.swing.JCheckBox interactiveCheck;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton printButton;
    private javax.swing.JTextArea text;
    // End of variables declaration//GEN-END:variables
}
